/**
 * 
 */
package com.ym;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;

import org.apache.log4j.Logger;

import com.alibaba.druid.util.Base64;


public class RsaUtil {
	private static final Logger logger = Logger.getLogger(RsaUtil.class);

	/**
	 * @throws NoSuchPaddingException
	 * @throws NoSuchAlgorithmException
	 * @Title: saveKey
	 * @Description: 获取并保存新的公钥私钥对
	 * @Return void
	 * @Throws
	 */
	public static void saveKey() throws Exception {
		KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");

		// 生成钥匙对
		KeyPair keyPair = keyPairGenerator.generateKeyPair();
		// 得到公钥
		Key publicKey = keyPair.getPublic();
		// 得到私钥
		Key privateKey = keyPair.getPrivate();

		// 保存公钥私钥
		ObjectOutputStream pubOos = new ObjectOutputStream(
				new FileOutputStream(new File("E://public.key")));
		ObjectOutputStream priOos = new ObjectOutputStream(
				new FileOutputStream(new File("E://private.key")));
/*		System.out.println("publicKey: " + publicKey);
		System.out.println("privateKey: " + privateKey);*/
		pubOos.writeObject(publicKey);
		priOos.writeObject(privateKey);
		try {
			if (pubOos != null) {
				pubOos.close();
			}
			if (priOos != null) {
				priOos.close();
			}
		} catch (Exception e) {
			logger.error("RsaUtil.saveKey Error:", e);
		}
	}

	/**
	 * @Title: encrypt
	 * @Description: RSA加密
	 * @param src 等待加密的内容
	 */
	public static String encrypt(String src) throws Exception {
		// 读取公钥
		Key publicKey = null;
		try {
			ObjectInputStream inputStream = new ObjectInputStream(
					new FileInputStream(new File("E://public.key")));
			publicKey = (Key) inputStream.readObject();
			if (inputStream != null) {
				inputStream.close();
			}
		} catch (Exception e) {
			logger.error("RsaUtil.encrypt Error:", e);
		}
		Cipher cipher = Cipher.getInstance("RSA");
		// 设置为加密模式
		cipher.init(Cipher.ENCRYPT_MODE, publicKey);
		// 对数据进行加密
		byte[] result = cipher.doFinal(src.getBytes());
		return Base64.byteArrayToBase64(result);
	}
	
	/**
	 * 
	* @Title: decrypt 
	* @Description: RSA解密
	* @param src 等待解密的内容
	 */
	public static String decrypt(String src) throws Exception {
		Key privateKey = null;
		try {
			ObjectInputStream inputStream = new ObjectInputStream(
					new FileInputStream(new File("E://private.key")));
			privateKey = (Key) inputStream.readObject();
			if (inputStream != null) {
				inputStream.close();
			}
		} catch (Exception e) {
			logger.error("RsaUtil.decrypt Error:", e);
		}
		Cipher cipher = Cipher.getInstance("RSA");
		cipher.init(Cipher.DECRYPT_MODE, privateKey);
		byte[] des = cipher.doFinal(Base64.base64ToByteArray(src));
		return new String(des);
	}
	
	
	
	public static void main(String[] args) throws Exception {
		saveKey();
		// Data must not be longer than 117 bytes
		String encrptSrc = encrypt("等待加密的内容1111111111111111111111111111111111111111111111111111111111111111111111可以再长一点么");
		System.out.println("加密后的密文:" + encrptSrc);
		
		String decryptSrc = decrypt(encrptSrc);
		System.out.println("解密后的密文:" + decryptSrc);
		
	}
}
